<?php
load()->func('tpl');
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if($op=='ajax_list')
{
	$gid = $_GPC['gid'];
	$orderno = $_GPC['orderno'];
	$jud_list = pdo_fetchall("select id,gid,gname,uniacid,id,openname,openid,avatar,status,judgment_id,item,FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s') as time from".tablename('tg_judgment')." where gid ='{$gid}' and orderno='{$orderno}'  and uniacid = {$_W['uniacid']} ORDER BY create_time desc");
	foreach($jud_list as &$li) {
		$main_content = pdo_fetch("select * from ".tablename('tg_judgment_content')." where  judgment_id=:judgment_id order by update_time asc ",array('judgment_id'=>$li['id']));
		$li['main_content'] = $main_content['content'];
		$allcontent = pdo_fetchall("select who,content,FROM_UNIXTIME(update_time,'%Y-%m-%d %H:%i:%s') as time from ".tablename('tg_judgment_content')." where  judgment_id=:judgment_id and content_id <> '". $main_content['content_id'] ."'  order by update_time asc ",array('judgment_id'=>$li['id']));
		$li['contents'] = $allcontent;
	}

	die(json_encode($jud_list));
}
if($op=='ajax_reply')
{
	$id = $_GPC['id'];
	$jud= pdo_fetch("select id,orderno,gid,gname,uniacid,id,openname,openid,avatar,status,judgment_id,item,FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s') as time from".tablename('tg_judgment')." where id ='{$id}' and uniacid = {$_W['uniacid']} ORDER BY create_time desc");
	
	$content = $_GPC['con'];	
	$data_content['judgment_id'] = $jud['id'];
	$data_content['content_id'] = date('Ymd').substr(time(), -5).substr(microtime(), 2, 5).sprintf('%02d', rand(0, 99));
	$data_content['content'] = $content;
	$data_content['update_time'] = TIMESTAMP;
	$data_content['who'] = 1;
	
	$addjudgment_content = pdo_insert('tg_judgment_content', $data_content);
	
	die(json_encode($jud));
}
if($op =='display'){
	if (!empty($_GPC['keyword_form_id'])) {
		$condition .= " and gname like '%{$_GPC['keyword_form_id']}%' ";
	}
	
	$pindex = max(1, intval($_GPC['page']));
	$psize = 10;

	$total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tg_judgment') . " where uniacid = {$_W['uniacid']} " .$condition. "  ORDER BY create_time DESC ");
	$pager = pagination($total, $pindex, $psize);
	
	$list = pdo_fetchall("select gname,uniacid,id,openname,openid,avatar,status,judgment_id,item,FROM_UNIXTIME(create_time,'%Y-%m-%d ') as time from".tablename('tg_judgment')." where uniacid = {$_W['uniacid']} " .$condition. " ORDER BY create_time desc LIMIT " . ($pindex - 1) * $psize . ',' . $psize,array());

	include wl_template('order/judgment_admin');
}
if($op =='replay'){
	$judgment_id = $_GPC['jid'];
	$content = $_GPC['content'];
	
	$data_content['judgment_id'] = $judgment_id;
	$data_content['content_id'] = date('Ymd').substr(time(), -5).substr(microtime(), 2, 5).sprintf('%02d', rand(0, 99));
	$data_content['content'] = $content;
	$data_content['update_time'] = TIMESTAMP;
	$data_content['who'] = 1;
	
	$addjudgment_content = pdo_insert('tg_judgment_content', $data_content);
	
	$goimages = $_GPC['img'];
	$goimages = array_reverse($goimages);
	foreach ($goimages as $key => $value) {
		$data2 = array('img_url' => $goimages[$key], 'content_id' => $data_content['content_id']);
		pdo_insert('tg_judgment_img', $data2);
	}	
	
	echo "<script>alert('回复成功');location.href='".web_url('order/judgment_admin',array('op' =>'display'))."';</script>";
	exit();
}
if($op =='delete'){
	pdo_query("delete from " . tablename('tg_judgment') . " where id= {$_GPC['id']}");
	$tip='删除成功';
	echo "<script>alert('".$tip."');location.href='".web_url('order/judgment_admin',array('op' =>'display'))."';</script>";
	exit();
}
exit();